package com.kl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.domain.CarDTO;
import com.kl.domain.LogisticsStoreDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Car;
import com.kl.feign.LogisticsStoreControllerFeign;
import com.kl.service.ICarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/car")
@Api(value = "物流站点管理功能接口")
@RefreshScope //nacos配置中心 开启自动刷新配置
public class CarController {

    @Resource
    private ICarService iCarService;

    //feign(奋) 声明式服务掉用 封装了restful 让调用HTTP更加简单
    @Resource
    private LogisticsStoreControllerFeign logisticsStoreControllerFeign;

    @ApiOperation("restful进行服务调用")
    @GetMapping("selectCarByIds/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "车辆ID",required = true, paramType = "path")
    })
    public Result<CarDTO> selectCarByIds(@PathVariable Long id){

        Result<LogisticsStoreDTO> logisticsStoreDTOResult = logisticsStoreControllerFeign.selectLogisticsStoreById(1L);
        System.out.println(logisticsStoreDTOResult);

        Car byId = iCarService.getById(id);

        Map<String,Object> map =new HashMap<>();
        map.put("LogisticsStore",logisticsStoreDTOResult);
        map.put("car",byId);

        return Result.build(ConstantEnum.SUCCESS).setResult(map);
    }


    @ApiOperation("根据ID进行查询")
    @GetMapping("selectCarById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "车辆ID",required = true, paramType = "path")
    })
    public Result<CarDTO> selectCarById(@PathVariable Long id){
        CarDTO carDTO = new CarDTO();

        Car byId = iCarService.getById(id);

        BeanUtils.copyProperties(byId,carDTO);

        return Result.build(ConstantEnum.SUCCESS).setResult(carDTO);
    }

    @ApiOperation("增加车辆")
    @PostMapping("insertCar")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carDTO",value = "车辆实体类",required = true, paramType = "body")
    })
    public Result insertCar(@RequestBody CarDTO carDTO){

        if (null == carDTO || StringUtils.isBlank(carDTO.toString())){
            return Result.build(ConstantEnum.ARGS_NULL);
        }
        Car car = new Car();
        BeanUtils.copyProperties(carDTO,car);
        car.insert();
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation("修改车辆信息(修改车辆状态)")
    @PutMapping("updateCar/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carDTO",value = "车辆实体类",required = false, paramType = "body"),
            @ApiImplicitParam(name="state",value = "车辆状态更新(当前状态1:空闲,2:运行中,3维修保养,4报废,)",required = false, paramType = "query"),
            @ApiImplicitParam(name="id",value = "车辆ID",required = true, paramType = "path")
    })
    public Result updateCar(@RequestBody CarDTO carDTO ,@PathVariable Long id
            ,@RequestParam(required = false) String state){

            Car car = new Car();
            car.setId(id);
            car.setCarLoad(carDTO.getCarLoad());
            car.setTodayCount(carDTO.getTodayCount());
            car.setState(state);
            car.setLineRange2(carDTO.getLineRange2());
            car.updateById();
            return Result.build(ConstantEnum.SUCCESS);

    }

    @ApiOperation("删除车辆信息(逻辑删除)")
    @DeleteMapping("deleteCar")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "实体类的ID(或数组)",required = true, paramType = "body")
    })
    public Result deleteCar(@RequestBody Long[] id){

        List<Long> list = new ArrayList();
        for (Long aLong : id) {
            list.add(aLong);
        }
        boolean b = iCarService.removeByIds(list);

        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation("车辆模糊查询分页")
    @GetMapping("selectCarLikePage")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNumber",value = "起始页数",required = false, paramType = "query"),
            @ApiImplicitParam(name="pageSize",value = "每页条数",required = false, paramType = "query"),
            @ApiImplicitParam(name="carType",value = "车的类型",required = false, paramType = "query"),
            @ApiImplicitParam(name="site",value = "所属站点",required = false, paramType = "query"),
            @ApiImplicitParam(name="state",value = "当前状态",required = false, paramType = "query"),
            @ApiImplicitParam(name="carDriver",value = "车辆司机",required = false, paramType = "query"),
    })
    public Result<Map<String,Object>> selectCarLikePage(@RequestParam(defaultValue = "1")Integer pageNumber
            , @RequestParam(defaultValue = "3")Integer pageSize
            , @RequestParam(required = false)Integer carType
            , @RequestParam(required = false)String site
            , @RequestParam(required = false)String state
            , @RequestParam(required = false)String carDriver){


        Map<String,Object> map = iCarService.selectCarLikePage(pageNumber,pageSize,carType,site,state,carDriver);
        return Result.build(ConstantEnum.SUCCESS).setResult(map);
    }

    @ApiOperation("批量修改车辆状态")
    @PutMapping("updateCarBatchState")
    @ApiImplicitParams({
            @ApiImplicitParam(name="state",value = "车辆状态",required = true, paramType = "query"),
            @ApiImplicitParam(name="id",value = "车辆ID",required = true, paramType = "body")
    })
    public Result updateCarBatchState(@RequestBody Long[] id,@RequestParam Integer state){

        List<Car> list = new ArrayList<>();
        for (Long aLong : id) {
            Car car = new Car();
            car.setId(aLong);
            car.setState(state.toString());
            list.add(car);
        }
        iCarService.updateBatchById(list);
        return Result.build(ConstantEnum.SUCCESS);
    }


    //kl增加
    @ApiOperation("查询所有by站点id")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "站点id",required = true, paramType = "query")
    })
    @GetMapping("toStateCar")
    public Result<List<CarDTO>> getBylogitIdToList(@RequestParam Long id){
       // Result<LogisticsStoreDTO> dtoResult = logisticsStoreControllerFeign.selectLogisticsStoreById(id);
        //String storeName = dtoResult.getResult().getStoreName();
        QueryWrapper<Car> war = new QueryWrapper<>();
        war.ge("site",id);
        //5为空车
        war.ge("state","5");
        List<Car> list = iCarService.list(war);

        if(list.size() <= 0){
            return Result.build(ConstantEnum.CAR_IS_NULL).setResult(null);
        }else{
            String s = JSON.toJSONString(list);
            List<CarDTO> carDTOS = JSONObject.parseArray(s, CarDTO.class);
            System.out.println("kong:"+carDTOS.size());
            return Result.build(ConstantEnum.SUCCESS).setResult(carDTOS);
        }


    }

   //kl增加修改byid
    @ApiOperation("修改byCarid")
    @ApiImplicitParams({
            @ApiImplicitParam(name="car",value = "车",required = true, paramType = "body")
    })

    @PutMapping("/updateByid")
    public Result getBylogitIdUpdate(@RequestBody CarDTO dto){
        String s = JSONObject.toJSONString(dto);
        Car car = JSONObject.parseObject(s, Car.class);
        boolean b = iCarService.updateById(car);
        if(b){
            return Result.build(ConstantEnum.SUCCESS);

        }else{
            return Result.build(ConstantEnum.ERROR);

        }
    }
}
